package online.longzipeng.householdbillssystem.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import online.longzipeng.householdbillssystem.business.entity.BillType;
import online.longzipeng.householdbillssystem.business.vo.bill.BillTypeSaveVO;
import online.longzipeng.householdbillssystem.business.vo.bill.BillTypeUpdateVO;
import online.longzipeng.householdbillssystem.common.result.ResponseEnum;
import online.longzipeng.householdbillssystem.common.result.Result;
import online.longzipeng.householdbillssystem.common.utils.ExceptionUtils;
import online.longzipeng.householdbillssystem.service.BillTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-17
 */
@RestController
@RequestMapping("/bill_type")
@Api(value = "账单类别相关接口",description = "账单类别相关接口",tags = "账单类别相关接口")
@Slf4j
public class BillTypeController {

    @Autowired
    private BillTypeService billTypeService;

    @PostMapping("/save")
    @ApiOperation("新建保存账单类别信息")
    public Result save(@Valid @RequestBody BillTypeSaveVO billTypeSaveVO){
        BillType billType = new BillType();
        BeanUtils.copyProperties(billTypeSaveVO,billType);
        if (log.isDebugEnabled()){
            log.info(String.format("新建账单类别成功,账单信息：%s",billType.toString()));
        }
        ExceptionUtils.isTrue(billTypeService.add(billType),"账单类别保存失败！！请检查填写的账单类别信息",billType);
        if (log.isDebugEnabled()){
            log.info(String.format("新建账单类别成功,账单信息：%s",billType.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @DeleteMapping("/delete_one/{id}")
    @ApiOperation("删除单个账单类别信息")
    public Result deleteOne(@ApiParam(value="账单id",required = true) @PathVariable("id") Integer id){
        ExceptionUtils.isTrue(billTypeService.delete(id),"删除失败！！参数不合法或已被其他用户删除",id);
        if (log.isDebugEnabled()){
            log.info(String.format("账单类别删除成功,账单信息：%d",id));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PostMapping("/delete")
    @ApiOperation("删除多个账单类别信息")
    public Result delete(@ApiParam(value="账单类别id数组",required = true, example = "1,2,3,4") @RequestParam("ids") Integer[] ids){
        for (Integer id : ids) {
            ExceptionUtils.isTrue(billTypeService.delete(id),"删除失败！！参数不合法或已被其他用户删除",ids);
        }
        if (log.isDebugEnabled()){
            log.info(String.format("账单类别批量删除成功，ids：%s",ids.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PutMapping("/update")
    @ApiOperation("修改账单类别信息")
    public Result update(@Valid @RequestBody BillTypeUpdateVO billTypeUpdateVO){
        BillType billType = new BillType();
        BeanUtils.copyProperties(billTypeUpdateVO,billType);
        ExceptionUtils.isTrue(billTypeService.edit(billType),"更新失败！！请检查输入的数据",billType);
        if (log.isDebugEnabled()){
            log.info(String.format("修改账单类别信息成功，账单类别信息：%s",billType.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @GetMapping("/find_all/{id}")
    @ApiOperation("查询用户所有的账单类型")
    public Result findAll(@ApiParam(value="用户id",required = true) @PathVariable("id") Integer id){
        List<BillType> billTypes = billTypeService.findAll(id);
        Result result = new Result(ResponseEnum.SUCCESS);
        result.setData(billTypes);
        return result;
    }

    @GetMapping("/find_default")
    @ApiOperation(value = "查询系统默认的账单类型",notes = "就是查询userId为0的账单类型")
    public Result findDefault(){
        List<BillType> billTypes = billTypeService.findDefault();
        Result result = new Result(ResponseEnum.SUCCESS);
        result.setData(billTypes);
        return result;
    }

    @GetMapping("/find_default_icon")
    @ApiOperation("查询系统默认的图标")
    public Result findDefaultIcon(){
        List<BillType> billTypes = billTypeService.findDefault();
        List<String> icons = null;
        if (!CollectionUtils.isEmpty(billTypes)) {
            icons = billTypes.stream().map(BillType::getImgUrl).collect(Collectors.toList());
        }
        Result result = new Result(ResponseEnum.SUCCESS);
        result.setData(icons);
        return result;
    }

    @GetMapping("/find_group/{id}")
    @ApiOperation(value = "查询用户组的的账单类别",notes = "用户组的账单类别就是创建该用户组的用户的类别")
    public Result findByGroupId(@ApiParam("用户组id") @PathVariable(value = "id",required = true) Integer id){
        List<BillType> billTypes = billTypeService.findByGroupId(id);
        Result result = new Result(ResponseEnum.SUCCESS);
        result.setData(billTypes);
        return result;
    }
}
